<template>
  <div class="flexContent">
    <!-- 门店调拨 -->
    <div class="searchBox">
      <el-form ref="form" label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="出库门店">
                  <el-select @click.native="getShopEasy" v-model="sform.allocationStore" popper-class="eloption" clearable
                    style="width: 95%" filterable placeholder="选择门店">
                    <el-option v-for="shop in shopList" :key="shop.id" :value="shop.id" :label="shop.name" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="入库门店">
                  <el-select @click.native="getShopEasy" v-model="sform.receiveStore" popper-class="eloption" clearable
                    style="width: 95%" filterable placeholder="选择门店">
                    <el-option v-for="shop in shopList" :key="shop.id" :value="shop.id" :label="shop.name" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="商品">
                  <el-input v-clearEmoji v-model.trim="sform.commodityName" size="small" clearable style="width: 95%;"
                    placeholder="请输入商品条码/名称/助记符" @keyup.enter.native="searchFn" />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="10" :xl="6">
                <el-form-item label="日期">
                  <el-date-picker v-model="date" clearable type="daterange" unlink-panels range-separator="-"
                    start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" style="width: 95%" />
                </el-form-item>
              </el-col>

            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="searchFn" class="searchBtn" icon="el-icon-search">搜索</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <div class="add">
      <el-button class="addBtn" @click="toAddPage" v-has="'allocation_add'">新增</el-button>
      <!-- <el-button class="addBtn" @click="toAddPage" v-has="'allocation_export'">导出</el-button> -->
    </div>
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" border class="tablebox" ref="xTable1"
          :row-config="{ isHover: true }" :scroll-y="{ enabled: true, gt: 21 }" style="width: 100%" height="100%"
          size="medium" align="center">
          <vxe-column type="seq" title="序号" width="80" />
          <vxe-column field="allocationNumber" title="调拨编号"
            :min-width="elTableAdaptationWidth('allocationNumber', tableData)" />
          <vxe-column field="allocationStoreName" title="出库门店"
            :min-width="elTableAdaptationWidth('allocationStoreName', tableData)" />
          <vxe-column field="receiveStoreName" title="入库门店" min-width="150" />
          <vxe-column field="userName" title="调拨人" min-width="150" />
          <vxe-column field="isInbound" title="是否入库" min-width="150">
            <template v-slot="{ row }">
              <el-tag v-if="row.isInbound == 0" type="info" size="small">未入库</el-tag>
              <el-tag v-if="row.isInbound == 1" size="small">已入库</el-tag>
            </template>
          </vxe-column>
          <vxe-column field="allocationTime" title="调拨时间" min-width="150">
            <template v-slot="{ row }">
              <span>{{ row.allocationTime | parseTime }}</span>
            </template>
          </vxe-column>
          <vxe-column fixed="right" title="操作" width="100" v-if="$showFn(['allocation_detail', 'allocation_print'])">
            <template slot-scope="scope">
              <el-button type="text" @click="toDetail(scope.row.id)" v-has="'allocation_detail'">详情</el-button>
              <el-button type="text" @click="toPrint(scope.row.id)" v-has="'allocation_print'">打印</el-button>
            </template>
          </vxe-column>
        </vxe-table>

      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,-> , sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>

<script>
import { getShopEasy, getAllocationList, getAllocationDetail } from '@/api/shop';
import { getSCMList } from "@/api/scm";
import { getBuyReturnsSelectist } from "@/api/warehouse";
import { printDbd, checkPrinter } from '@/utils/printLODOP';
export default {
  name: "PurchaseBack",
  data() {
    return {
      isSearch: false,
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      date: [],
      sform: {
        allocationStore: "",
        maxDate: "",
        minDate: "",
        receiveStore: "",
        commodityName: ''
      },
      supplierList: [],
      tableData: [],
      shopList: [],
      storeId: ''
    };
  },
  created() {
    // this.getSCMList();
    this.getBuyReturnsSelectist();
  },
  methods: {
    async getShopEasy() {
      const res = await getShopEasy();
      if (res.code === 200) {
        console.log(res);
        const result = res.data;
        console.log('shopList', result);
        this.shopList = result.store;
        console.log('shopList', this.shopList);
      } else {
        // this.$message.error(res.message);
      }
    },
    async getSCMList() {
      const res = await getSCMList();
      if (res.code === 200) {
        this.supplierList = res.data.list;
        console.log(this.supplierList, "supplierList");
      }
    },
    async getBuyReturnsSelectist() {
      if (!this.isSearch) {
        this.sform = {
          allocationStore: "",
          maxDate: "",
          minDate: "",
          receiveStore: "",
          commodityName: ''
        };
      }
      const res = await getAllocationList({ ...this.page, ...this.sform });
      if (res.code === 200) {
        this.tableData = res.data.list;
        this.page.pageSize = res.data.pageSize;
        this.page.pageNum = res.data.pageNum;
        this.page.total = res.data.total;
        console.log(res.data, "退货单");
        const $table = this.$refs.xTable1
        $table.loadData(this.tableData);
      }
    },
    resetForm(formName) {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.sform = {
        allocationStore: "",
        maxDate: "",
        minDate: "",
        receiveStore: "",
        commodityName: ''
      };
      this.isSearch = false;
      this.date = [];
      this.getBuyReturnsSelectist();
    },
    toAddPage() {
      this.$router.push({
        path: "commodityallocationAdd",
      });
    },
    toDetail(id) {
      console.log(id);
      this.$router.push({
        path: "commodityallocationDetail",
        query: {
          id: id,
        },
      });
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      console.log(`每页 ${val} 条`);

      this.getBuyReturnsSelectist();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      this.page.pageNum = val;

      this.getBuyReturnsSelectist();
    },
    async searchFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };

      this.isSearch = true;
      this.sform.maxDate = this.date?.length ? this.date[1] : "";
      this.sform.minDate = this.date?.length ? this.date[0] : "";
      this.getBuyReturnsSelectist();
    },
    toPrint(id) {
      this.$confirm(`即将打印调拨单，是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        if (!checkPrinter()) return;
        const res = await getAllocationDetail({ id });
        if (res.code === 200) {
          this.printObj = res.data;
          // console.log(res.data,"调拨单数据");
          printDbd(this.printObj);
          this.$message({ type: "success", message: "正在打印调拨单" });
        }
      });
    }
  },
};
</script>

<style lang="scss" scoped></style>
